package cn.m1c.cntt.wuhan2020.cnttlifegoods.person.controller

import cn.m1c.cntt.wuhan2020.cnttlifegoods.common.component.Security
import cn.m1c.cntt.wuhan2020.cnttlifegoods.common.enums.StatusCode
import cn.m1c.cntt.wuhan2020.cnttlifegoods.common.model.ResultVo
import cn.m1c.cntt.wuhan2020.cnttlifegoods.common.utils.AssertUtil
import cn.m1c.cntt.wuhan2020.cnttlifegoods.common.utils.getAuthUser
import cn.m1c.cntt.wuhan2020.cnttlifegoods.person.service.UserService
import cn.m1c.cntt.wuhan2020.cnttlifegoods.person.vo.UserVo
import cn.m1c.cntt.wuhan2020.cnttlifegoods.person.vo.UserWxVo
import com.alibaba.fastjson.JSON
import org.apache.logging.log4j.kotlin.Logging
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.redis.core.RedisTemplate
import org.springframework.data.redis.core.StringRedisTemplate
import org.springframework.http.MediaType
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.*
import javax.annotation.Resource
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

@Controller
@RequestMapping("/user")
@CrossOrigin(origins = ["*"])
class UserController: Logging {

    @Autowired
    lateinit var userService: UserService

    @Resource
    lateinit var redisTemplate : RedisTemplate<UserVo, String>

    @Resource
    lateinit var stringRedisTemplate: StringRedisTemplate

    @PostMapping("/login" )
    @ResponseBody
    fun login(request: HttpServletRequest, response: HttpServletResponse, @RequestBody userVo: UserVo):String{
        var resultVo = userService.login(userVo)
        return JSON.toJSONString(resultVo)
    }
    @PostMapping("/loginByWx")
    @ResponseBody
    fun loginByWx(request: HttpServletRequest, response: HttpServletResponse, @RequestBody userwxVo: UserWxVo):String{
        AssertUtil.hasLengthByTrim(userwxVo.openId, StatusCode.failed,"openid不能为空")
        var resultVo = userService.loginWx(userwxVo)
        return JSON.toJSONString(resultVo)
    }
    @PostMapping("/register" )
    @ResponseBody
    fun register(request: HttpServletRequest, response: HttpServletResponse, @RequestBody userVo: UserVo):String{
        var resultVo = userService.register(userVo)
        return JSON.toJSONString(resultVo)
    }
    @GetMapping("/info" )
    @ResponseBody
    @Security
    fun info(request: HttpServletRequest, response: HttpServletResponse):String{
        var userVo = getAuthUser(request)
        return JSON.toJSONString(ResultVo<Map<String,String>>(data = mapOf<String,String>("nickName" to userVo.nickName!!,"phone" to userVo.phone!!)))
    }


}